home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / KEYCOMP.C < prev    next >
C/C++ Source or Header  |  1986-12-01  |  5KB  |  201 lines

  1. /******************************************************************************
  2.  
  3.  KEYCOMP.C
  4.  
  5.  (C) Copyright 1986   Marc Adler   All Rights Reserved
  6.  
  7.  
  8.    Reads a file (arg1) whose line are of the form
  9.      key=func
  10.  
  11.    Key can be a decimal number, 'char', ^char (control char),
  12.    F# (normal func key), AF# (ALT func key), SF# (shift func key),
  13.    CF# (CTRL func key), @char (ALT key)
  14.  
  15.    Func can be a decimal number or a character of the form 'c' or c.
  16.  
  17.    This program will generate a 256 byte "compiled" keystroke file called
  18.    KEYTABLE.ME. Byte n will contain the translated char that will be read
  19.    by ngetc() when key n is pressed.
  20.  
  21. ******************************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <fcntl.h>
  26. #include "keys.h"
  27.  
  28. /*** BASE VALUES FOR FUNCTION KEY 0 ***/
  29. #define NORM_FUNC   186
  30. #define SHIFT_FUNC  211
  31. #define CTRL_FUNC   221
  32. #define ALT_FUNC    231
  33.  
  34. typedef struct keymap
  35. {
  36.   struct keymap *nextmap;
  37.   int    prefix;
  38.   unsigned char keytable[256];
  39. } KEYMAP;
  40.  
  41. struct _alts
  42. {
  43.   char *altchars;
  44.   int  altbase;
  45. } alts[5] =
  46. {
  47.   "QWERTYUIOP", 144,
  48.   "ASDFGHJKL",  158,
  49.   "ZXCVBNM",    172,
  50.   "12345678",   248,
  51.   "90-+",       128
  52. };
  53.  
  54.  
  55. KEYMAP *alloc_keymap(),
  56.        *find_keymap();
  57. KEYMAP *MainKeyMap;
  58. int    NumMaps = 0;
  59.  
  60.  
  61. main(argc, argv)
  62.   int  argc;
  63.   char **argv;
  64. {
  65.   char *fgets();
  66.   FILE *f, *fopen();
  67.   unsigned char buf[100], c;
  68.   char *s, *t, *strrchr();
  69.   char *kp;
  70.   int  i, k, outf;
  71.   KEYMAP *kmap, *lastmap;
  72.   long magic;
  73.  
  74.   int  debug = 1;
  75.  
  76.   if (argc < 2)
  77.     f = stdin;
  78.   else if ((f = fopen(argv[1], "r")) == NULL)
  79.   {
  80.     fprintf(stderr, "Can't open input file %s\n", argv[1]);
  81.     exit(1);
  82.   }
  83.  
  84.   /* init the table with byte[n] = n */
  85.   MainKeyMap = kmap = lastmap = alloc_keymap(0);
  86.   for (kp = MainKeyMap->keytable, i = 0;  i < 256;  *kp++ = i++)  ;
  87.  
  88.   while (fgets(buf, 100, f))
  89.   {
  90.     /* skip any comments and blank lines */
  91.     if (*buf == '#' || isspace(*buf))
  92.       continue;
  93.  
  94.     /* convert left half to all upper case (if not in quotes) */
  95.     for (t = buf;  *t && *t != '\'';  t++)
  96.       *t = toupper(*t);
  97.  
  98.     if (!strncmp(buf, "PREFIX", 6))
  99.     {
  100.       for (s = buf+6;  *s && isspace(*s);  s++)  ;
  101.  
  102.       k = decode_key(s);
  103.       MainKeyMap->keytable[k] = KEYPREFIX;
  104.       kmap = alloc_keymap(k);
  105.       lastmap->nextmap = kmap;
  106.       lastmap = kmap;
  107.       continue;
  108.     }
  109.  
  110.     /* Separate left and right hand sides */
  111.     if ((s = strrchr(buf, '=')) == NULL)
  112.       continue;
  113.     else
  114.       *s = '\0';
  115.  
  116.     if ((k = decode_key(buf)) >= 0)    /* fill in the entry */
  117.     {
  118.       kmap->keytable[k] = c = decode_key(s+1);
  119.       if (isalpha(k) &&        /* Upper & lower case entries should be same */
  120.           kmap != MainKeyMap)  /*  in prefix commands (ie ^KD == ^Kd).      */
  121.         kmap->keytable[toupper(k)] = c;
  122.     }
  123.   }
  124.  
  125.   /* Write out the keymaps */
  126.   if ((outf = open("keytable.me", O_CREAT|O_TRUNC|O_WRONLY|O_RAW, 0)) >= 0)
  127.   {
  128.     magic = KEYMAGIC;
  129.     write(outf, &magic, sizeof(magic));
  130.     write(outf, &NumMaps, sizeof(NumMaps));
  131.     for (kmap = MainKeyMap;  kmap;  kmap = kmap->nextmap)
  132.     {
  133.       write(outf, &kmap->prefix, sizeof(kmap->prefix));
  134.       write(outf, kmap->keytable, sizeof(char) * 256);
  135.     }
  136.   }
  137. }
  138.  
  139.  
  140. decode_key(buf)
  141.   char *buf;
  142. {
  143.   int  c, k;
  144.  
  145.   if (isdigit(c = buf[0]))           /* numerical key */
  146.     k = atoi(buf);
  147.   else if (c == '@')                 /* ALT keystroke */
  148.     k = find_alt(buf[1]);
  149.   else if (c == '\'')                /* quoted char  */
  150.     k = buf[1];
  151.   else if (c == '^')                 /* control char */
  152.     k = buf[1] & 0x1F;
  153.   else if (c == 'F')                 /* regular function key */
  154.     k = NORM_FUNC + atoi(buf+1);
  155.   else if (buf[1] == 'F')
  156.   {
  157.     if (c == 'A')                    /* AF = ALT func key */
  158.       k = ALT_FUNC;
  159.     else if (c == 'S')               /* SF = SHIFT func key */
  160.       k = SHIFT_FUNC;
  161.     else if (c == 'C')               /* CF - CTRL func key */
  162.       k = CTRL_FUNC;
  163.     k += atoi(buf+2);                /* should be '1' - '10' */
  164.   }
  165.   else
  166.     k = c;
  167.  
  168.   if (isalpha(k) && k < 127)  k = tolower(k);
  169.  
  170.   return(k);
  171. }
  172.  
  173. /* find_alt - takes care translating of '@char' sequence */
  174. find_alt(c)
  175.   char c;
  176. {
  177.   int  i;
  178.   char *s, *strchr();
  179.  
  180.   /* search the string in alt[i] for the char - if it's in, return */
  181.   /* the base value plus its position in the string.               */
  182.   for (i = 0;  i < 5;  i++)
  183.     if ((s = strchr(alts[i].altchars, c)) != NULL)
  184.       return (int) (alts[i].altbase + (s - alts[i].altchars));
  185.   return 0;
  186. }
  187.  
  188. KEYMAP *alloc_keymap(prefix)
  189.   unsigned prefix;
  190. {
  191.    KEYMAP *k;
  192.    char   *calloc();
  193.  
  194.    k = (KEYMAP *) calloc(sizeof(KEYMAP), 1);
  195.    k->nextmap = NULL;
  196.    k->prefix = prefix;
  197.    NumMaps++;
  198.    return(k);
  199. }
  200.  
  201.